1 package tw.com.javaworld.CH19;
2 
3 import java.io.*;
4 import java.util.*;
5 import javax.servlet.*;
6 import javax.servlet.http.*;
7 
8 public class ShoppingServlet extends HttpServlet {
9     public void init(ServletConfig conf) throws ServletException {
10        super.init(conf);
11    }
12    public void doPost(HttpServletRequest req, HttpServletResponse res)
13        throws ServletException, IOException {
14        HttpSession session = req.getSession(false);
15
16        // 若session為naull時,程式將導向 Error.html
17        if (session == null) {
18            res.sendRedirect("Error.html");
19        }
20        Vector buylist = (Vector) session.getAttribute("shoppingcart");
21        String action = req.getParameter("action");
22
23        if (!action.equals("CHECKOUT")) {
24
25            //  刪除購物車中的書籍
26            if (action.equals("DELETE")) {
27                String del = req.getParameter("del");
28                int d = (new Integer(del)).intValue();
29                buylist.removeElementAt(d);
30            }
31            // 新增書籍至購物車中
32            else if (action.equals("ADD")) {
33                boolean match = false;
34
35                // 取得後來新增的書籍
36                Book abook = getBook(req);
37
38                //新增第一本書籍至購物車時
39                if (buylist == null) {
40                    buylist = new Vector();
41                    buylist.addElement(abook);
42                } else {
43                    for (int i = 0; i < buylist.size(); i++) {
44                        Book book = (Book) buylist.elementAt(i);
45
46                        // 假若新增的書籍和購物車的書籍一樣時
47                        if (book.getName().equals(abook.getName())) {
48                            book.setQuantity(
49                                book.getQuantity() + abook.getQuantity());
50                            buylist.setElementAt(book, i);
51                            match = true;
52                        } //end of if name matches
53                    } // end of for
54
55                    // 假若新增的書籍和購物車的書籍不一樣時
56                    if (!match)
57                        buylist.addElement(abook);
58                }
59            }
60
61            session.setAttribute("shoppingcart", buylist);
62            String url = "/CH19/EShop.jsp";
63            ServletContext sc = getServletContext();
64            RequestDispatcher rd = sc.getRequestDispatcher(url);
65            rd.forward(req, res);
66        }
67
68        // 結帳,計算購物車書籍價錢總數
69        else if (action.equals("CHECKOUT")) {
70            float total = 0;
71            for (int i = 0; i < buylist.size(); i++) {
72                Book order = (Book) buylist.elementAt(i);
73                float price = order.getPrice();
74                int quantity = order.getQuantity();
75                total += (price * quantity);
76            }
77
78            String amount = new Float(total).toString();
79            req.setAttribute("amount", amount);
80            String url = "/CH19/Checkout.jsp";
81            ServletContext sc = getServletContext();
82            RequestDispatcher rd = sc.getRequestDispatcher(url);
83            rd.forward(req, res);
84        }
85    }
86
87    private Book getBook(HttpServletRequest req) {
88        
89        String name = encoding(req.getParameter("name"));
90        String quantity = encoding(req.getParameter("quantity"));
91        String author = encoding(req.getParameter("author"));
92        String publisher = encoding(req.getParameter("publisher"));
93        String price = encoding(req.getParameter("price"));
94        
95        Book bk = new Book();
96
97        bk.setName(name);
98        bk.setAuthor(author);
99        bk.setPublisher(publisher);
00        bk.setPrice((new Float(price)).floatValue());
01        bk.setQuantity((new Integer(quantity)).intValue());
02        return bk;
03    }
04    
05    private String encoding(String str) {
06    
07        try {
08            str = new String(str.getBytes("ISO-8859-1"), "MS950");
09        } catch (UnsupportedEncodingException uee) {
10            System.out.println("UnsupportedEncodingException:" + uee.getMessage());
11        }
12        
13        return str;
14    }
15}
16